<%@page import="java.net.URLEncoder"%>
<%@page import="models.AutoSite"%>
<%@page import="helpers.StringHelper"%>
<%@page import="java.util.ArrayList"%>
<%@page import="models.User"%>
<%@page import="models.Blog"%>
<%@page import="java.util.TreeMap"%>
<%
	TreeMap check = (TreeMap) request.getAttribute("check");
	String success = (String) request.getAttribute("success");
	String form = (String) request.getAttribute("form");
	Blog blog = (Blog) request.getAttribute("blog");
	ArrayList<Blog> blogs = (ArrayList<Blog>) request.getAttribute("blogs");
	ArrayList<AutoSite> sites = (ArrayList<AutoSite>) request.getAttribute("sites");
	User user = (User) request.getAttribute("user");
%>

<jsp:include page="/layouts/header.jsp">
	<jsp:param name="title" value="" />
	<jsp:param name="description" value="" />
	<jsp:param name="keywords" value="" />
	<jsp:param name="page" value="home" />
</jsp:include>

<div id="userform" class="clearafter">
	<h1><%=form.equalsIgnoreCase("edit") ? "Edit" : "Add" %> Blog</h1><hr/>
	<div class="submitlink">
		<form method="post" action="" name="submitlink">
			<%=success != null ? "<p class=\"msg-success\">" + success + "</p>" : "" %>
			<% if (check.get("limitBlog") != null) { %>
					<span class="error"><%=check.get("limitBlog")%></span><br />
			<% } %>
			<% if (form != null && form.equals("edit")) { %>
				<a onclick="javascript:if(!confirm('Do you want delete ?'))return false;" style="font-size: 20px;" href="/delete-blog/<%=blog.getBlogId()%>">Delete</a>
			<% } %>
			<p>
				<label> 
					Username
					<br /> 
					<b class="font-24"><%=user.getGmail() %></b>
				</label>
			</p>
			<p>
				<% if (check.get("type") != null) { %>
					<span class="error"><%=check.get("type")%></span><br />
				<% } %>
				<label> 
					Choose the type of your blog
					<br /> 
				</label>
				<div class="typelogo">
					<img title="wordpress logo" alt="wordpress logo" src="/images/logo-wordpress.png" width="100" height="100">
					<label>
					<input class="input type" type="radio" name="type" style="width: auto;"  value="wordpress" 
						<%=form != null && form.equalsIgnoreCase("edit") && !blog.getType().equalsIgnoreCase("wordpress")? "disabled=\"disabled\"" : ""%>
						<%=blog.getType().equalsIgnoreCase("wordpress") ? "checked=\"checked\"" : "" %>/>
					Wordpress 
					</label>
				</div>
				<div class="typelogo">
					<img title="blogspot logo" alt="blogspot logo" src="/images/logo-blogspot-orange.png" width="100" height="100">
					<label>
					<input class="input type" type="radio" name="type" style="width: auto;" value="blogger" 
						<%=form != null && form.equalsIgnoreCase("edit") && !blog.getType().equalsIgnoreCase("blogger")? "disabled=\"disabled\"" : ""%>
						<%=blog.getType().equalsIgnoreCase("blogger") ? "checked=\"checked\"" : "" %>/>
					Blogger
					</label>
				</div>
				<div class="clearbreak"></div>
			</p>
			<p>
				<% if (check.get("blogId") != null) { %>
					<span class="error"><%=check.get("blogId")%></span><br />
				<% } %>
				<label> 
					<span class="txt-blogger">BlogSpot ID</span>
					<span class="txt-wordpress">Blog Url</span>
					<% if (form.equalsIgnoreCase("add")) { %>
					<img class="question" src="/images/i-help.gif" alt="Question">
					<span class="note">Input BlogSpotId at your BlogSpot</span>
					<br /> <input class="input" type="text" name="blogId" id="blogId" value="<%=blog.getBlogId() != null ? blog.getBlogId() : ""%>"/> 
					<% } else { %>
						<br />
						<b class="font-24"><%=blog.getBlogId() != null ? blog.getBlogId() : ""%></b>
						<input type="hidden" name="blogId" id="blogId" value="<%=blog.getBlogId() != null ? blog.getBlogId() : ""%>"/> 
					<% } %>
				</label>
			</p>
			<p>
				<% if (check.get("blogName") != null) { %>
					<span class="error"><%=check.get("blogName")%></span><br />
				<% } %>
				<label> 
					<span class="txt-blogger">Blog Url</span>
					<span class="txt-wordpress">Blog Name</span>
					<img class="question" src="/images/i-help.gif" alt="Question">
					<span class="note">Input blog name</span>
					<br /> <input class="input" type="text" name="blogName" id="blogName" value="<%=blog.getBlogName() != null ? blog.getBlogName() : ""%>"/> 
				</label>
			</p>
			<p>
				<% if (check.get("username") != null) { %>
					<span class="error"><%=check.get("username")%></span><br />
				<% } %>
				<label> 
					<span class="txt-blogger">Email</span>
					<span class="txt-wordpress">Blog's username </span>
					<img class="question" src="/images/i-help.gif" alt="Question">
					<span class="note">Input your username that you registed at BlogSpot. </span>
					<br /> <input class="input" type="text" name="username" id="b_username" value="<%=blog.getUsername() != null ? blog.getUsername() : ""%>"/> 
				</label>
			</p>
			<p>
				<% if (check.get("password") != null) { %>
					<span class="error"><%=check.get("password")%></span><br />
				<% } %>
				<label> 
					<span class="txt-blogger">Password</span>
					<span class="txt-wordpress">Blog's password </span>
					<img class="question" src="/images/i-help.gif" alt="Question">
					<span class="note">Input your password that you registed at BlogSpot.</span>
					<br /> <input class="input" type="password" name="password" id="b_password" value="<%=blog.getPassword() != null ? blog.getPassword() : ""%>"/> 
				</label>
			</p>
			<p>
				<label> Status <br />
					<select name="status" class="input type" >
						<option value="0" <%=blog.getStatus()==0 ? "selected=\"selected\"" : "" %>>Inactive</option>
						<option value="1" <%=blog.getStatus()==1 ? "selected=\"selected\"" : "" %>>Active</option>
					</select>
				</label>
			</p>
			<p>
				<% if (check.get("category") != null) { %>
					<span class="error"><%=check.get("category")%></span><br />
				<% } %>
				<label> 
					Category
					<img class="question" src="/images/i-help.gif" alt="Question">
					<span class="note">Input your password that you registed at BlogSpot.</span>
					<br /> <input class="input" type="text" name="category" id="b_category" value="<%=!blog.getCategories().isEmpty() ? StringHelper.listToString(blog.getCategories(), ",") : ""%>"/> 
				</label>
			</p>
			<p>
				<% if (check.get("limitSite") != null) { %>
					<span class="error"><%=check.get("limitSite")%></span><br />
				<% } %>
				<label> 
					Maximum site in a blog: 
					<img class="question" src="/images/i-help.gif" alt="Question">
					<span class="note">Maximum source site content for blog.</span>
					<br /> 
					<b class="font-24"><%=blog.getLimitSite()%></b>
					<input class="input" type="hidden" name="limitSite" id="b_limitSite" value="<%=blog.getLimitSite()%>"/> 
				</label>
			</p>
			<div id='load-field'>
				
			</div>
			<p>
				<br />
				<input type="button" class="check-info button" value="Check info" />
				<input type="submit" class="disabled submit-blog" value="<%=form != null && form.equalsIgnoreCase("edit") ? "Update" : "Submit" %>"/>
			</p>
		</form>
	</div>
	
	<div class="howdy">
		<h2 class="title">Hi, <%=user.getName() != null ? user.getName() : "" %></h2>
		<ul class="option">
			<% if (form.equalsIgnoreCase("edit"))  { %>
			<li><a href="/add-blog">Add blog</a></li>
			<li><a href="/add-site?blogId=<%=blog.getBlogId()%>">Add site</a></li>
			<li><a href="/view-blog/<%=blog.getBlogId()%>">View Blog</a></li>
			<li><a href="/get-content?blogId=<%=blog.getBlogId()%>">Get Special Content</a></li>
			<% }%>
			<li><a href="/view-profile">View profile</a></li>
			<li><a href="/logout">Logout</a></li>
		</ul>
		
		<% if (form.equalsIgnoreCase("add"))  { %>
		<h2 class="title" style="padding-top: 20px;">List blogs</h2>
		<div class="listlink">
			<ul>
				<% for (int i = 0; i < blogs.size(); i++) { %>
					<li> <a class="<%=blogs.get(i).getType() != null && blogs.get(i).getType().equalsIgnoreCase("wordpress") ? "icon-wordpress" : "icon-blogger" %><%=blogs.get(i).getStatus()==0 ? " inactive" : "" %>" 
							href="/edit-blog/<%=blogs.get(i).getBlogId()%>"><%=blogs.get(i).getBlogName() %> (<%=blogs.get(i).getBlogId() %>)</a></li>
				<% } %>
			</ul>
		</div>
		<% } else { %>
		<h2 class="title" style="padding-top: 20px;">List sites</h2>
		<div class="listlink">
			<ul>
				<% for (int i = 0; i < sites.size(); i++) { %>
					<li><a class="site-icon <%=sites.get(i).getStatus()==0 ? "inactive" : "" %>" style="background-image: url('https://s2.googleusercontent.com/s2/favicons?domain_url=<%=sites.get(i).getSite()%>');"
							href="/view-site?siteId=<%=URLEncoder.encode(sites.get(i).getId())%>"><%=sites.get(i).getSite()%></a></li>
				<% } %>
			</ul>
		</div>
		<% } %>
	</div>
	<div class="clearbreak"></div>
	<hr />
	<div class="checkresult">
	</div>
</div>
<script type="text/javascript">
<!--
var action = "<%=form%>";
//-->
</script>
<script type="text/javascript" src="/js/form_blog.js"></script>
<%@ include file='/layouts/footer.jsp'%>